package hz.mobileproject.pda.ui.activity.work;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.request.Request_getGridPerson;
import hz.mobileproject.pda.entity.response.Response_gridPerson_item;
import hz.mobileproject.pda.entity.response.Response_noneProcess_item;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.ui.adapter.GridPersonsAdapter;
import hz.mobileproject.pda.utils.Base64;

public class GridPersonActivity extends Activity implements View.OnClickListener {
    private TextView tv_title;
    private ListView lv_gridPerson;
    private Button btn_handle;

    private Response_noneProcess_item np_item;
    private Gson mGson;
    private GridPersonsAdapter gpAdapter;
    private ArrayList<Response_gridPerson_item> listData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_person);
        initView();
        initData();
        initListener();
    }


    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title_assets);
        lv_gridPerson = (ListView) findViewById(R.id.lv_grid_person);
        btn_handle = (Button) findViewById(R.id.btn_handle);
    }
    private void initData() {
        mGson = new Gson();
        np_item = (Response_noneProcess_item) getIntent().getSerializableExtra("np");
        LogUtils.e(np_item.getCus_number()+"<---------->"+np_item.getTerm_id());
        tv_title.setText(np_item.getTerm_name()+"成员");
        gpAdapter = new GridPersonsAdapter(this,new ArrayList<Response_gridPerson_item>());
        getPersonList();
    }
    private void initListener() {
        lv_gridPerson.setAdapter(gpAdapter);
        btn_handle.setOnClickListener(this);
    }

    /**
     * 获取小组成员列表请求
     */
    private void getPersonList() {
        Request_getGridPerson gdp = new Request_getGridPerson(np_item.getCus_number(),np_item.getTerm_id());
        String gdpStr = mGson.toJson(gdp);
        LogUtils.e(gdpStr);
        Request_data data = new Request_data();
        data.setData(Base64.getBase64(gdpStr));
        String body_data = mGson.toJson(data);

        RequestParams params = new RequestParams(Constance.BASE_URL + "/" + Constance.PROJECT_NAME +  "/grid/loadMember");
        params.setBodyContent(body_data);
        x.http().post(params, callback);
    }

    /**
     * 获取小组成员回调
     */
    private Callback.CommonCallback<String> callback = new Callback.CommonCallback<String>() {
        @Override
        public void onSuccess(String result) {
            try {
                JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
                String returnStr = Base64.getFromBase64(returnData.get("data").getAsString());
                LogUtils.e(returnStr);
                listData = mGson.fromJson(returnStr,
                        new TypeToken<ArrayList<Response_gridPerson_item>>() {}.getType());
                if(listData ==null || listData.size()==0){
                    Toast.makeText(GridPersonActivity.this,"没有更多数据了",Toast.LENGTH_LONG).show();
                    return;
                }
                gpAdapter.setListData(listData);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_handle://处理事件按钮
                finish();
                Intent it = new Intent(this,GridChoosePeopleActivity.class);
                it.putExtra("np_item",np_item);
                it.putExtra("grid_person",listData);
                startActivity(it);
                break;
        }
    }
}
